using Commons.Utils;

namespace Commons.Models.Functions
{
	public class StepFunction : AbstractFunction<double, double>
	{
		private RangeDictionary<double, double> values;

		public StepFunction(string pointsCsv, string variable)
			: base(variable)
		{
			values = new RangeDictionary<double, double>(true);

			string[] pointsArray = pointsCsv.Split(';');
			foreach (string point in pointsArray)
			{
				if (point.Length > 0)
				{
					string[] coordinates = point.Split(',');
					double x = double.Parse(coordinates[0]);
					double y = double.Parse(coordinates[1]);
					values.Add(x, y);
				}
			}
		}

		public override double Eval(double x)
		{
			return values.Get(x);
		}
	}
}

